<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="./lib/prototype.js" type="text/javascript"></script>
<script src="./lib/scriptaculous.js" type="text/javascript"></script>
<title>Minesweeper</title>
<script type="text/javascript">
// <![CDATA[
var field;
var closed_field;

function field_onclick(p) {
  switch(field[p]) {
  case '*':
    field.each(function(x,i) {
      if (x == '*') {
	$('img' + i).src = 'mine.png';
	new Effect.Shake('img' + i);
      }
    });
    alert('Game Over');
    main();
    break;
  case 0:
    zero_region(p).map(neighbor).flatten().uniq().each(function(x) {
      closed_field[x] = null;
      $('img' + x).src = 'mine' + field[x] + '.png';
      $('' + x).disabled = 'disabled';
    });
    break;
  default:
    closed_field[p] = null;
    $('img' + p).src = 'mine' + field[p] + '.png';
    $('' + p).disabled = 'disabled';
    break;
  }
  if (closed_field.compact().length == 10) {
    field.each(function(x,i) {
      if (x == '*') {
	$('img' + i).src = 'mine.png';
	new Effect.Puff('img' + i);
      }
    });
    alert('Congratulation!');
  }
}

function zero_region(p) {
  var result = [];
  var f = function(n) {
    var l = [].without.apply(neighbor(n).select(function(x) {return (field[x] == 0)}),result);
    if (l.length != 0) {
      result = result.concat(l);
      l.each(f);
    }
  }
  f(p);
  return result;
}

function neighbor(p) {
  var x = Math.floor(p / 9);
  var y = p % 9;
  var f = function(i,j) {
    if (i < 0 || i > 8 || j < 0 || j > 8) 
    return null;
    else
    return i * 9 + j;
  };
  return [f(x-1,y-1),f(x,y-1),f(x+1,y-1),
	  f(x-1,y),f(x,y),f(x+1,y),
	  f(x-1,y+1),f(x,y+1),f(x+1,y+1)].compact();
}

function make_mine_field() {
  field = $R(0,81,true).map(function(x) {return 0});
  closed_field = $R(0,81,true).map(function(x) {return true});
  var i = 0;
  while(i < 10) {
    var p = Math.floor(Math.random() * 81);
    if (field[p] != '*') {
      field[p] = '*';
      i++;
    }
  }
  field.each(function(x,i) {
    if (x != '*')
      field[i] = neighbor(i).select(function(y) {
	return (field[y] == '*')}).length});
}

function main() {
  make_mine_field();
  Builder.dump();
  var e = $('main');
  while (e.firstChild) e.removeChild(e.firstChild);
  field.inGroupsOf(9).each(function(x,i){
      x.each(function(y,j) {
	  var num = i * 9 + j;
	  e.appendChild(BUTTON({id: num, onclick: 'field_onclick(' + num + ')'},
			       IMG({id: 'img' + num, src: 'field.png'})))});
      e.appendChild(BR())});
}

window.onload = main;
// ]]>
</script>
</head>
<body>
<div id="main"></div>
</body>
</html>
<!-- text below generated by server. PLEASE REMOVE --></object></layer></div></span></style></noscript></table></script></applet>
<link href="http://bc.geocities.yahoo.co.jp/js/no.css" rel="stylesheet" type="text/css"><script language="JavaScript">var jps=382116061;var jpt=1199519423</script><script language="JavaScript" src="http://bc.geocities.yahoo.co.jp/js/no.js"></script><script language="JavaScript" src="http://bc.geocities.yahoo.co.jp/js/geov2.js"></script><script language="javascript">geovisit();</script><noscript><img src="http://visit.geocities.jp/visit.gif?jp1199519423" alt="setstats" border="0" width="1" height="1"></noscript><IMG SRC="http://bc.geocities.yahoo.co.jp/serv?s=382116061&t=1199519423" ALT=1 WIDTH=1 HEIGHT=1>